| 1 |  |  | import { eventbus } from '@/lib/EventBus'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import { GIT_FILE_ADDED_EVENT } from '@/lib/Events'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import { createOctokit, Github } from '@/lib/github'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import { parseFullRepositoryName } from '@/lib/stringHelpers'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import { RepositoryInfo } from '@/types/RepositoryInfo'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import { execSync } from 'child_process'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import { existsSync, mkdirSync } from 'fs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import { dirname } from 'path'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  | import simpleGit, { Response, SimpleGit, SimpleGitTaskCallback } from 'simple-git'; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | export class Repository { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     public name: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     public owner: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public path: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public gitInstance!: SimpleGit; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     public get info() { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         return <RepositoryInfo>{ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             owner: this.owner, | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             name: this.name, | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     public get git() { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         if (this.gitInstance === undefined) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             this.gitInstance = simpleGit({ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |                 baseDir: this.path, | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |                 binary: 'git', | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                 maxConcurrentProcesses: 3, | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                 trimmed: false, | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             }) as SimpleGit; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         return this.gitInstance; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public initGitListeners(runId: string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         this.git.add = (files: string | string[], callback?: SimpleGitTaskCallback<string>): Response<string> => { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             eventbus.emit(GIT_FILE_ADDED_EVENT, { repository: this.info, files, runId }); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             return this.gitInstance.raw([ 'add', ...[].concat(<any>files) ], callback); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     constructor(fullRepositoryName: string, repositoryStoragePath: string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         const { owner, name } = parseFullRepositoryName(fullRepositoryName); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         this.name = name; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         this.owner = owner; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         this.path = `${repositoryStoragePath}/${fullRepositoryName}`; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public async clone() { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         const parentDir = dirname(this.path); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         if (!existsSync(parentDir)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             mkdirSync(parentDir, { recursive: true }); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         if (!existsSync(this.path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             execSync(`git -C '${parentDir}' clone [email protected]:${this.owner}/${this.name}.git`, { stdio: 'pipe' }); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * Checks out the default branch and pulls down the latest changes | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     public async prepare() { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             await this.checkout(await this.defaultBranch()); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             await this.git.pull('origin'); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         } catch (e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             console.log('Error preparing repository: ', e); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     public async localBranches() { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         const result = await this.git.branchLocal(); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         return result; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     public async currentBranch() { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         return (await this.localBranches()).current; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     public async onBranch(branchName: string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         return (await this.currentBranch()) === branchName; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     public async checkout(branchName: string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         const branchList = await this.localBranches(); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         if (branchList.current === branchName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         if (branchList.all.includes(branchName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             await this.git.checkout(branchName); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         await this.git.checkoutLocalBranch(branchName); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     public async defaultBranch() { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         const result = await this.git.revparse('--abbrev-ref', [ 'origin/HEAD' ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         return result.replace(/^.+\//, ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     public async createFork() { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         const octokit = createOctokit(); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |             const result = await Github.forkRepository(this.info, octokit); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |             this.git.addRemote('fork', result.ssh_url); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |         } catch (e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             console.error(e); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |             const result = await Github.getRepository(this.info, octokit); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             this.git.addRemote('fork', result.ssh_url); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         } catch (e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             console.error(e); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     async pushToFork(branchName: string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |         await this.git.push('fork', branchName); | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     fullRepositoryName() { | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         return `${this.owner}/${this.name}`; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  |